package com.lvtulife.paas.gtd.service.impl;

import com.lvtulife.common.bean.DelVo;
import com.lvtulife.common.component.constants.Constant;
import com.lvtulife.common.component.exception.ErrMsg;
import com.lvtulife.paas.gtd.bean.AimVo;
import com.lvtulife.paas.gtd.entity.GtdIdeal;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import com.lvtulife.paas.gtd.mapper.GtdAimMapper;
import com.lvtulife.paas.gtd.entity.GtdAim;
import com.lvtulife.paas.gtd.service.GtdAimServiceI;
import com.lvtulife.common.service.impl.BaseServiceImpl;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;

/**
 * GtdAim 表数据服务层接口实现类
 */
@Service
@Transactional(readOnly = true)
public class GtdAimServiceImpl extends BaseServiceImpl<GtdAimMapper, GtdAim> implements GtdAimServiceI {

    @Override
    @Transactional(readOnly = false, propagation = Propagation.REQUIRED)
    public Long saveAim(AimVo vo) {
        GtdAim entity = initBean(vo);

        int count = vo.getId() != null ? baseMapper.updateSelectiveById(entity) : baseMapper.insert(entity);
        checkOperation(count);

        return entity.getId();
    }

    @Override
    @Transactional(readOnly = false, propagation = Propagation.REQUIRED)
    public void starAim(Long id) {
        GtdAim entity = getEntity(id);
        int star = entity.getStar().equals(Constant.Gtd.Star.YES) ? Constant.Gtd.Star.NO : Constant.Gtd.Star.YES;

        entity = new GtdAim(id);
        entity.setStar(star);
        checkOperation(baseMapper.updateSelectiveById(entity));
    }

    @Override
    @Transactional(readOnly = false, propagation = Propagation.REQUIRED)
    public void changeStatus(Long id, Integer status) {
        GtdAim entity = getEntity(id);

        /* 状态一致 */
        if (entity.getAimStatus().equals(status)) {
            return;
        }

        switch (status) {
            case Constant.Gtd.AimState.WAIT:
                changeWait(entity, status);
                break;
            case Constant.Gtd.AimState.COMPLETED:
                changeCompleted(entity, status);
                break;
            case Constant.Gtd.AimState.CANCEL:
                changeCancel(entity, status);
                break;
            default:
                throw new RuntimeException(ErrMsg.INVALID_CONTENT.getMsg());
        }

        /* 更新状态 */
        entity = new GtdAim(entity.getId());
        entity.setAimStatus(status);
        checkOperation(updateSelectiveById(entity));
    }

    @Override
    @Transactional(readOnly = false, propagation = Propagation.REQUIRED)
    public void delAim(DelVo delVo) {
        verifyParams(delVo);
        delEntity(delVo.getId(), delVo.getUserId());
    }

    @Override
    @Transactional(readOnly = false, propagation = Propagation.REQUIRED)
    public void multiDelAim(DelVo delVo) {
        verifyParams(delVo);

        for (Long id : delVo.getIds()) {
            delEntity(id, delVo.getUserId());
        }
    }

    /* 设置未达成状态 */
    private void changeWait(GtdAim entity, Integer status) {
        entity.setActualEndDt(null);
    }

    /* 设置已达成状态 */
    private void changeCompleted(GtdAim entity, Integer status) {
        if (entity.getAimStatus().equals(Constant.Gtd.AimState.CANCEL)) throw new RuntimeException("当前信息为作废状态,不允许更改成已达成状态");

        entity.setActualEndDt(new Date());//完成时间
    }

    /* 设置作废状态 */
    private void changeCancel(GtdAim entity, Integer status) {
        if (entity.getAimStatus().equals(Constant.Gtd.AimState.COMPLETED)) throw new RuntimeException("当前信息为已达成状态,不允许作废");

        entity.setActualEndDt(new Date());//作废时间
    }

    private void delEntity(Long id, Long userId) {
        verifyParams(id, userId);

        GtdAim entity = getEntity(id);

        if (!entity.getUid().equals(userId)) {
            throw new RuntimeException(ErrMsg.PERMISSION_DENIED.getMsg());
        }

        delEntity(id);
    }

    private void delEntity(Long id) {
        GtdAim entity = new GtdAim(id);
        entity.setDel(Constant.Base.State.DEL);
        checkOperation(updateSelectiveById(entity));
    }

    private GtdAim initBean(AimVo vo) {
        verifyParams(vo);

        GtdAim entity = null;
        if (vo.getId() != null) {
            entity = verifyEntity(new GtdAim(vo.getId()));

            if (vo.getType() != null) {
                verifyConstant("Gtd.AimType", vo.getType());
                entity.setAimType(vo.getType());
            }

            /* 可修改项 */
            if (StringUtils.isBlank(vo.getContent()))
                entity.setContent(vo.getContent());
            if (StringUtils.isBlank(vo.getDemand()))
                entity.setDemand(vo.getDemand());
            if (StringUtils.isBlank(vo.getRemark()))
                entity.setRemarks(vo.getRemark());

            /* 时间有效性 */
            if (vo.getStartDt() != null && vo.getEndDt() != null) {
                entity.setStartDt(vo.getStartDt());
                entity.setEndDt(vo.getEndDt());
            }
        } else {
            verifyParams(vo.getType(), vo.getGroup(), vo.getUserId(), vo.getContent(), vo.getDemand());


            entity = new GtdAim();

            /* 验证参数合法性 */
            verifyConstant("Gtd.AimGroup", vo.getGroup());
            verifyConstant("Gtd.AimType", vo.getType());

            /* 必填项 */
            entity.setAtGroup(vo.getGroup());
            entity.setAimType(vo.getType());
            entity.setUid(vo.getUserId());
            entity.setContent(vo.getContent());
            entity.setDemand(vo.getDemand());

            /* 默认项 */
            entity.setAimStatus(Constant.Gtd.AimState.WAIT);
            entity.setStar(vo.isStar() ? Constant.Gtd.Star.YES : Constant.Gtd.Star.NO);
            entity.setDel(Constant.Base.State.NORMAL);
            entity.setAimNo("");
            entity.setDid(0L);
        }

        return entity;
    }
}